//
//  PMControl.h
//
//  Copyright (C) 2007 Visual.Wei
//  Author: Wei Shuai <cpuwolf@sina.com>
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
//

#include <windows.h>
#include "PMList.h"



//------------------------------------------------------------------------------------------------
HIMAGELIST PMList::SetImageList(HIMAGELIST himl)
{
	return ListView_SetImageList(g_hwnd,himl,LVSIL_SMALL);
}
int PMList::AutoSizeColumn(int index)
{
	return ListView_SetColumnWidth(g_hwnd,index,LVSCW_AUTOSIZE);
}

int PMList::AddCol(int index,TCHAR * txt,int len)
{
	LVCOLUMN col;
	static int sindex=0;

	memset(&col,0,sizeof(col));
	col.mask=LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	col.fmt=LVCFMT_LEFT;
	col.pszText=txt;
	col.cchTextMax=len;
	col.cx=100;
	col.iSubItem=sindex;
	sindex++;
	return ListView_InsertColumn(g_hwnd,index,&col);
}

int PMList::AddItem(int index,TCHAR * txt,int len)
{
	LVITEM itm;

	memset(&itm,0,sizeof(itm));
	itm.iItem=index;
	itm.mask=LVIF_TEXT|LVIF_IMAGE ;
	itm.pszText=txt;
	itm.cchTextMax=len;
	itm.iSubItem=0;
	itm.iImage=0;
	return ListView_InsertItem(g_hwnd,&itm);
}

int PMList::AddSubItem(int index,int subindex,TCHAR * txt)
{
	int ret=TRUE;
	ListView_SetItemText(g_hwnd,index,subindex,txt);
	return ret;
}

void PMList::DeleteChild(TVITEM * pitem)
{
	if(pitem)
		if(pitem->mask|TVIF_PARAM)
			if(pitem->lParam)
				free((void *)(pitem->lParam));
}
void PMList::SelectChange(LPNMTREEVIEW tv)
{


}
DWORD PMList::Notify(int idCtrl,LPNMHDR pnmh)
{
	LPNMTREEVIEW tv;
	tv=(LPNMTREEVIEW)pnmh;
	POINT pt;
	switch(pnmh->code)
	{
		case TVN_SELCHANGED:
			SelectChange(tv);
			break;
		case TVN_DELETEITEM:
			DeleteChild(&tv->itemOld);
			break;
		case NM_RCLICK:
			GetCursorPos(&pt);
			return RightMouseClick(pt.x,pt.y);
	}
	return TRUE;
}
DWORD PMList::RightMouseClick(LONG x,LONG y)
{
	return TRUE;
}
DWORD PMList::ReSize()
{
	RECT rect;
	GetClientRect(g_hpwnd,&rect);
	//SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, FALSE);	
	//rect.top+=5;
	//MoveWindow(g_hwnd,rect.left,rect.top,rect.right-rect.left,p_pmsplit->WndRect.top-rect.top,true);
	return true;
}

PMList::PMList(HWND hWnd):PMControl()
{
	RECT rect;

	GetClientRect(hWnd,&rect);
	g_hwnd = CreateWindowEx ( WS_EX_CLIENTEDGE,  WC_LISTVIEW, TEXT("List View"),
	                           WS_CHILD | LVS_REPORT   | LVS_EDITLABELS|LVS_SORTASCENDING|LVS_AUTOARRANGE|LVS_SINGLESEL  ,
	                           0, rect.top,rect.right,rect.bottom, hWnd,(HMENU)NULL,g_hInst, NULL);
	
	Update();

	//SendMessage(g_hwnd,(UINT) LVM_SETVIEW,(WPARAM) LV_VIEW_LIST,  (LPARAM) 0);  
}
//------------------------------------------------
